// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/core/expressions.h>

namespace kumo::pollux::functions::prestosql {
    struct SimpleComparison {
        core::TypedExprPtr expr;
        bool isLessThen;
    };

    /// Given a lambda expression, checks it if represents a simple comparator and
    /// returns the summary of the same.
    ///
    /// For example, identifies
    ///     (x, y) -> if(length(x) < length(y), -1, if(length(x) > length(y), 1, 0))
    /// expression as a "less than" comparison over length(x).
    ///
    /// Recognizes different variations of this expression, e.g.
    ///
    ///     (x, y) -> if(expr(x) = expr(y), 0, if(expr(x) < expr(y), -1, 1))
    ///     (x, y) -> if(expr(x) = expr(y), 0, if(expr(y) > expr(x), -1, 1))
    ///
    /// Returns std::nullopt if expression is not recognized as a simple comparator.
    ///
    /// Can be used to re-write generic lambda expressions passed to array_sort into
    /// simpler ones that can be evaluated more efficiently.
    std::optional<SimpleComparison> isSimpleComparison(
        const std::string &prefix,
        const core::LambdaTypedExpr &expr);
} // namespace kumo::pollux::functions::prestosql
